using System;
using System.Data;
using System.Data.SqlClient;
using NNS.Framework;
using QLTB.BusinessObjects;

namespace QLTB.DataAccess
{
    /// <summary>
    /// Project: NNS System 
    /// Generated Class for Table : TBL_quyen.
    /// Date: 06/03/2010
    /// Author: Trần Hoàng Tâm
    /// </summary>
    public class TBL_quyenDA : Object
    {
        #region Get

        public TBL_quyen Get(
            string menu,
            int idnhom)
        {
            TBL_quyen quyen = null;
            SqlDataReader myReader = (SqlDataReader) SqlHelper.Helper.ExecuteReader(CommandType.Text,
                                                                                    "select * from TBL_quyen Where Menu='" + menu +
                                                                                    "' and idnhom=" + idnhom);
            if (myReader == null)
            {
                SqlHelper.Helper.Close();
                return null;
            }
            if (myReader.Read())
            {
                quyen = Populate(myReader);
            }
            myReader.Close();
            SqlHelper.Helper.Close();
            return quyen;
        }

        #endregion

        public TBL_quyen Populate(
            IDataReader myReader)
        {
            TBL_quyen obj = null;
            if (myReader != null)
            {
                obj = new TBL_quyen();
                if (!myReader.IsDBNull(myReader.GetOrdinal("id")))
                {
                    obj.id = (int) myReader["id"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("idnhom")))
                {
                    obj.idnhom = (int) myReader["idnhom"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("menu")))
                {
                    obj.menu = (string) myReader["menu"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("xem")))
                {
                    obj.xem = (bool) myReader["xem"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("thaydoi")))
                {
                    obj.thaydoi = (bool) myReader["thaydoi"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("taomoi")))
                {
                    obj.taomoi = (bool) myReader["taomoi"];
                }
                if (!myReader.IsDBNull(myReader.GetOrdinal("tatca")))
                {
                    obj.tatca = (bool) myReader["tatca"];
                }
            }
            return obj;
        }

        #region Update

        public int Update(
            int id,
            int idnhom,
            string menu,
            bool xem,
            bool thaydoi,
            bool taomoi,
            bool tatca)
        {
            return SqlHelper.Helper.ExecuteNonQuery(CommandType.StoredProcedure, "TBL_quyen_Update",
                                                    new[]
                                                        {
                                                            new SqlParameter("@id", id),
                                                            new SqlParameter("@idnhom", idnhom),
                                                            new SqlParameter("@menu", menu), new SqlParameter("@xem", xem),
                                                            new SqlParameter("@thaydoi", thaydoi),
                                                            new SqlParameter("@taomoi", taomoi),
                                                            new SqlParameter("@tatca", tatca)
                                                        });
        }

        #endregion
    }
}